package io.github.weiranyi.studyjedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class JedisDemo01ConnectJedis {
    @Test
    public void openCAP() {
        Jedis jedis = new Jedis("192.168.182.129", 6379);
        jedis.set("yue","1000");

        int yue = Integer.parseInt(jedis.get("yue"));
        int zhichu = 100;
        jedis.watch("yue");//监控余额
        if (yue<zhichu){
            jedis.unwatch();// 解除监控
            System.out.println("余额不足");
        }else {
            Transaction transaction = jedis.multi(); //开启事务
            transaction.decrBy("yue",zhichu); // 余额减少
            transaction.incrBy("zhichu",zhichu);//累计支出增加
            transaction.exec();
            System.out.println(jedis.get("yue")+","+jedis.get("zhichu"));
        }

    }

    @Test
    public void useRedisApi() {
        Jedis jedis = new Jedis("192.168.182.129", 6379);
        // 1.先清空数据
        /*
            WRONGTYPE Operation against a key holding the wrong kind of value
         */
        jedis.flushAll();

        // 2.设置值
        jedis.set("k1", "v1");
        jedis.set("k2", "v2");
        // 3.得到key
        Set<String> set = jedis.keys("*");
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            // 4.得到值
            System.out.println(key + "->" + jedis.get(key));
        }
        //5. 判断key存在与否
        System.out.println("k1是否存在：" + jedis.exists("k1"));
        System.out.println("k1的过期时间是：" + jedis.ttl("k1")); // -1永不过期 -2已经过期

        //6.String类型
        jedis.mset("k3", "v3", "school", "cdut");
        System.out.println(jedis.mget("k3", "school"));
        //7.list类型
        jedis.lpush("list01", "l1", "l2", "l3", "l4");
        System.out.println(jedis.lrange("list01", 0, -1));
        //8.set类型
        jedis.sadd("order", "tb001");
        jedis.sadd("order", "tb002");
        jedis.sadd("order", "tb003");
        System.out.println(jedis.smembers("order"));
        jedis.srem("order", "tb003");
        System.out.println(jedis.smembers("order").size());
        //9.hash类型
        Map map = new HashMap<>();
        map.put("uid", "user01");
        map.put("username", "weiranyi");
        jedis.hset("user01", map);
        jedis.hmset("user02", map);
        System.out.println(jedis.hget("user01", "username"));
        System.out.println(jedis.hmget("user02", "uid", "username"));
        //10.zset类型
        jedis.zadd("stu", 90d, "zs1");
        jedis.zadd("stu", 80d, "zs2");
        jedis.zadd("stu", 95d, "zs3");
        Set<String> stu = jedis.zrange("stu", 0, -1);
        for (String s : stu) {
            System.out.println(s);
        }
    }

    @Test
    public void ConnectTest() {
        Jedis jedis = new Jedis("192.168.182.129", 6379);
        String result = jedis.ping();
        System.out.println("pong" + result); //PONG
    }

}
